<?php 
/*=======================================================================
// File:	JPGRAPH_VECTOR.PHP
// Description: Vector  plot extension for JpGraph
// Created: 	
// Author:	
// Ver:		
//

//========================================================================
*/
require_once ('jpgraph_plotmark.inc.php');


//===================================================
// CLASS VectorArrow
// Description: Draw an arrow at (x,y) with angle a
//===================================================
class VectorArrow {
    public $iColor='black';
    public $iStyle='solid';
    public $iWeight=2;
    public $iArrowSize = 5;
    private $isizespec = array(
	array(2,1),array(3,2),array(4,3),array(6,4),array(7,4),array(8,5),array(10,6),array(12,7),array(16,8),array(20,10));
    function FieldArrow() {
    }

    function SetSize($aArrowSize) {	
	$this->iArrowSize = $aArrowSize;
    }
    
    function SetWeight($aWeight) {
	$this->iWeight=$aWeight;
    }

    function SetColor($aColor) {
	$this->iColor = $aColor;
    }
    
    function SetStyle($aStyle) {
	$this->iStyle = $aStyle;
    }

    function Stroke($aImg,$x1,$y1,$x2,$y2) {
	$aImg->SetLineWeight($this->iWeight);
	
	$aImg->SetColor($this->iColor);
	$aImg->SetLineStyle($this->iStyle);
	$c = array($x1,$y1,$x2,$y2);//line               
	$aImg->Polygon($c);
        
        
        $aImg->SetLineWeight(1);
	
	$a = -180/M_PI*atan2($y2-$y1,$x2-$x1);
	// First rotate the center coordinates
	list($x,$y) = $aImg->Rotate($x2,$y2);

	$old_origin = $aImg->SetCenter($x2,$y2);
	$old_a = $aImg->a;
	
	$aImg->SetAngle(-$a+$old_a);

	//$dx = round($this->iSize/2);
	$dx = $x2 - $x1;
	$dx = $y2 - $y1;
	//$c = array($x-$dx,$y,$x+$dx,$y);//line
	
	//$x += $dx;

	list($dx,$dy) = $this->isizespec[$this->iArrowSize];
	//$ca = array($x,$y,$x-$dx,$y-$dy,$x-$dx,$y+$dy,$x,$y); //arrow
	$ca = array($x2,$y2, 
                    $x2-round(2.3*$dx),$y2-$dy, 
                    $x2-round(2.1*$dx),$y2, 
                    $x2-round(2.3*$dx),$y2+$dy, 
                    $x2,$y2); 

	
	
	$aImg->FilledPolygon($ca);

	$aImg->SetCenter($old_origin[0],$old_origin[1]);
	$aImg->SetAngle($old_a);
	
	
    }
}

//===================================================
// CLASS VectorPlot
// Description: Render a field plot
//===================================================
class VectorPlot extends Plot {
    //private $iAngles;
    private $iCallback='';
    function VectorPlot($datay,$datax) {
	if( (count($datax) != count($datay)) )
	    JpGraphError::RaiseL(20001);//("Fieldplots must have equal number of X and Y points.");
	
	if( (count($datax) != count($datay)) )
	    JpGraphError::RaiseL(20001);//("Fieldplots must have equal number of X and Y points.");
	
	
	
	//$this->iAngles = $angles;

	$this->Plot($datay,$datax);
	$this->value->SetAlign('center','center');
	$this->value->SetMargin(15);

	$this->arrow = new VectorArrow();
    }

    function SetCallback($aFunc) {
	$this->iCallback = $aFunc;
    }

    function Stroke($img,$xscale,$yscale) {

	// Remeber base color and size
	$bc = $this->arrow->iColor;
//	$bs = $this->arrow->iSize;
	
	$bas = $this->arrow->iArrowSize;

	$i=0;    
	while( $i<$this->numpoints ) {
	    // Skip null values
	    if( $this->coords[0][$i]==="" )
		continue;
            
	    $xstartt = $xscale->Translate($this->coords[1][$i]);
	    $ystartt = $yscale->Translate($this->coords[0][$i]);	
	    ++$i;
	    $xendt = $xscale->Translate($this->coords[1][$i]);
	    $yendt = $yscale->Translate($this->coords[0][$i]);	
	    ++$i;
	    $this->arrow->Stroke($img,$xstartt,$ystartt,$xendt,$yendt);
	    //$this->value->Stroke($img,$this->coords[0][$i],$xt,$yt);
	     
	}
    }
	
    // Framework function
    function Legend($aGraph) {
	if( $this->legend != "" ) {
	    $aGraph->legend->Add($this->legend,$this->mark->fill_color,$this->mark,0,
				 $this->legendcsimtarget,$this->legendcsimalt);
	}
    }	
}

/* EOF */
?>